package com.example.demo.Service;

import com.example.demo.Entity.*;
import com.example.demo.Mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ResumeServiceImpl implements ResumeService {

    @Autowired private PersonalInfoMapper userMapper;
    @Autowired private EducationMapper educationMapper;
    @Autowired private WorkExperienceMapper workExperienceMapper;
    @Autowired private CampusExperienceMapper campusExperienceMapper;
    @Autowired private SkillMapper skillMapper;
    @Autowired private EvaluationMapper evaluationMapper;

    // 查询方法
    @Override
    public PersonalInfo getUser(Long id) {
        return userMapper.findById(id);
    }

    @Override
    public List<PersonalInfo> getAll() {
        return userMapper.findAll();
    }

    @Override
    public List<Education> getEducations(Long userId) {
        return educationMapper.findByUserId(userId);
    }

    @Override
    public List<WorkExperience> getWorkExperiences(Long userId) {
        return workExperienceMapper.findByUserId(userId);
    }

    @Override
    public List<CampusExperience> getCampusExperiences(Long userId) {
        return campusExperienceMapper.findByUserId(userId);
    }

    @Override
    public List<Skill> getSkills(Long userId) {
        return skillMapper.findByUserId(userId);
    }

    @Override
    public Evaluation getEvaluation(Long userId) {
        return evaluationMapper.findByUserId(userId);
    }

    // 更新方法
    @Override
    @Transactional
    public void updateavatar(String avatar_path, Long id) {
        userMapper.updatePath(avatar_path, id);
    }

    @Override
    @Transactional
    public void insertUser(PersonalInfo personalInfo) {
        userMapper.insert(personalInfo);
    }

    @Override
    @Transactional
    public void deleteUser(Long id) {
        // 由于数据库设置了级联删除，相关记录会自动删除
        userMapper.delete(id);
    }

    @Override
    @Transactional
    public void updateUser(PersonalInfo personalInfo, Long id) {
        personalInfo.setId(id); // 确保ID一致
        userMapper.update(personalInfo);
    }

    @Override
    @Transactional
    public void updateEducations(Long userId, List<Education> educations) {

        educationMapper.update(userId,educations);
    }

    @Override
    public void addEducation(Education education) {

    }

    @Override
    public void deleteEducation(Long educationId) {

    }

    @Override
    @Transactional
    public void updateWorkExperiences(Long userId, List<WorkExperience> experiences) {
        // 先删除用户的所有工作经历
        workExperienceMapper.deleteByUserId(userId);

        // 插入新的工作经历
        for (WorkExperience exp : experiences) {
            exp.setPerson_id(userId);
            workExperienceMapper.insert(exp);
        }
    }

    @Override
    public void addWorkExperience(WorkExperience workExperience) {

    }

    @Override
    public void deleteWorkExperience(Long workExperienceId) {

    }

    @Override
    @Transactional
    public void updateCampusExperiences(Long userId, List<CampusExperience> experiences) {
        // 先删除用户的所有校园经历
        campusExperienceMapper.deleteByUserId(userId);

        // 插入新的校园经历
        for (CampusExperience exp : experiences) {
            exp.setPerson_id(userId);
            campusExperienceMapper.insert(exp);
        }
    }

    @Override
    public void addCampusExperience(CampusExperience campusExperience) {

    }

    @Override
    public void deleteCampusExperience(Long campusExperienceId) {

    }

    @Override
    @Transactional
    public void updateSkills(Long userId, List<Skill> skills) {
        // 先删除用户的所有技能
        skillMapper.deleteByUserId(userId);

        // 插入新的技能
        for (Skill skill : skills) {
            skill.setPerson_id(userId);
            skillMapper.insert(skill);
        }
    }

    @Override
    public void addSkill(Skill skill) {

    }

    @Override
    public void deleteSkill(Long skillId) {

    }

    @Override
    @Transactional
    public void updateEvaluation(Evaluation evaluation) {
        // 先删除旧的自我评价
        evaluationMapper.deleteByUserId(evaluation.getPerson_id());

        // 插入新的自我评价
        evaluationMapper.insert(evaluation);
    }
}